An Introduction To Developing From the Command Line in Linux
This tutorial is a beginner's guide to writing code in Linux using just command-line tools instead of full-featured IDEs.
Nov 27th, 2024 11:00am by
Featured image via Unsplash.
cc demo.c -o demo
./demo
Hello, New Stack!
What You Need To Develop on Linux
Obviously, you need a running instance of Linux. This can be any distribution because the tools will be available, no matter what flavor of Linux you use, from standard repositories. There are, however, exceptions to this. For example, the version of Java found in most standard repositories will be out of date, which means you’ll need to add a repository that includes the latest releases. What about other languages? Let’s take a look.- Python – pre-installed on most Linux distributions.
- C/C++ – can be installed from the standard repositories. On Fedora-based distributions, it can be added with the command sudo dnf groupinstall ‘Development Tools’ and on Ubuntu-based tools, that command is sudo apt-get install build-essential -y.
- Go – The Go binary can be downloaded from https://go.dev. Once downloaded, unpack the archive with a command like this:
- sudo tar -C /usr/local/ -xzf goXXX.linux-amd64.tar.gz (where XXX is the release number). You then must set the path for Go by adding the following line to the bottom of your ~/.profile file:
- export PATH=$PATH:/usr/local/go/bin.
- Java – How you install Java will depend on the version you need, but you can install the default package from the standard repositories with one of these commands: Ubuntu – sudo apt-get install default-jdk -y or Fedora – sudo dnf install <openjdk-package-name>. You can locate the package you want to install with dnf search openjdk.
- Node.js – On Ubuntu, install Node.js with sudo apt-get install nodejs -y and on Fedora, the command is: sudo :dnf install nodejs -y
Choose Your Editor
I’m just going to say this: Nano has been my editor of choice for a very long time. I know it’s not the popular choice, but I find it never gets in my way of doing what needs to be done. That being said, most serious developers who use Linux tend to prefer the old-school (and supremely powerful) vi or emacs. The thing about both of those editors is they each have a learning curve. Let me explain the workflow for vi.- Open a terminal window.
- Start vi with the command vi.
- Switch out of command mode into insert mode by pressing the i key on your keyboard.
- Type your code.
- Save and quit by first pressing the Escape key on your keyboard and then type :wq (for write/quit).
- Open a terminal window.
- Start nano with the command nano.
- Type your code.
- Save and quit with the Ctrl-X key combination.
Git ‘Er Done
At some point, you’ll need a version control system, especially if you work with a team. Fortunately, you can interact with Git from the command line, so there’s no need for a GUI there either. Git can be installed from the standard repositories, such as with the command:
sudo apt-get install git -y
- Create a new repository – mkdir ~/new-project
- Initialize the repository – git init .
- Add files to the repository – you can either copy them or create them from scratch.
- Connect the local repository with the GitHub repository with the command: git remote add origin URL (where URL is the address of the GitHub repository)
- Pull the content of the remote repository with: git pull origin master
- Create a README file and then add it with git add README
- Make your first commit with git commit -m “Added new information to README file”
- Push to send the changes to the remote repository with git push
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.