Version control is essential for modern software development, enabling developers to track changes, revert to previous versions, and create alternate branches of their code.
Git, a widely adopted version control system, facilitates this process. Many projects utilize Git repositories, and platforms like GitHub, GitLab, and Bitbucket foster collaboration and sharing within the development community.
This guide will demonstrate how to install and configure Git on an Ubuntu 18.04 server, exploring two installation methods: using the built-in package manager and compiling from source.
Each approach offers distinct advantages depending on specific needs.
Prerequisites
To successfully complete this tutorial, you will need access to an Ubuntu 18.04 server. You should also have a non-root user account with sudo
privileges configured on this server.
With your server and user account properly configured, you are ready to proceed with the tutorial.
Installing Git via packages
You can quickly install Git using Ubuntu’s default repositories. However, be aware that the version installed through this method may not be the most recent release.
If you require the very latest version of Git, please proceed to the next section of this tutorial, which covers installing and compiling Git from source.
First, use the apt package management tools to update your local package index:
sudo apt update
With the update complete, you can download and install Git:
sudo apt install git
You can confirm that you have installed Git correctly by running the following command:
git --version
Output
git version 2.17.1
With the Git installation now complete, you can move on to the next section of this tutorial, titled Git set up, to finalize the configuration process.
Installing Git from source code
An alternative method for installing Git is to compile it from source code. While this approach requires more time and manual updates compared to using the package manager, it provides greater flexibility.
You can obtain the latest release directly and customize the installation process by selecting specific options during compilation.
Verify the version of Git currently installed:
git --version
If Git is installed, you’ll receive output similar to the following:
Output
git version 2.17.1
Prior to compiling Git from source, it is necessary to install the required software dependencies. These dependencies are readily available within the default repositories. Begin by updating your local package index:
sudo apt update
Then install the packages:
sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc
Once you have successfully installed the required dependencies, navigate to the tmp
directory. This temporary directory will serve as the location for downloading the Git source code tarball.
cd /tmp
From the Git project website, you can navigate to the tarball list available at https://mirrors.edge.kernel.org/pub/software/scm/git/ and download the version you of your choosing. Use curl
and output the file downloaded to git.tar.gz
.
curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.37.1.tar.gz
Unpack the compressed tarball file:
tar -zxf git.tar.gz
Next, move into the new Git directory:
cd git-*
At this point, you can proceed with building the Git package and installing it on your system by executing the following two commands:
make prefix=/usr/local all
sudo make prefix=/usr/local install
Now, replace the shell process so that the version of Git you just installed will be used:
exec bash
To ensure that the Git installation was successful, you can verify the installed version by executing the following command:
git --version
Output
git version 2.37.1
With Git successfully installed, you can now complete your setup.
Git set up
To ensure that your Git commit messages accurately reflect your identity and assist in project development, it is essential to configure Git with your name and email address.
Git incorporates this information into each commit you create. Utilize the git config
command to set your name and email as follows:
git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"
Display all of the configuration items that have been set by typing:
git config --list
Output
user.name=Your Name
user.email=youremail@domain.com
...
The information you provide is stored within your Git configuration file. While you can manually edit this file using your preferred text editor, the following example demonstrates how to use the nano
text editor.
nano ~/.gitconfig
~/.gitconfig contents
[user]
name = Your Name
email = youremail@domain.com
You can press CTRL + X
, then Y
then ENTER
to exit the nano
text editor.
While numerous other configuration options are available, setting your name and email address are the most essential. If you omit this step, Git will generate warnings during the commit process. This necessitates revisiting and revising previously committed changes to include the correct information.
Conclusion
Git is a powerful tool for tracking changes, reverting to previous versions, and creating different versions of files and directories. Through this tutorial, you have learned how to install Git on your Ubuntu 18.04 server and configure the essential settings for effective Git usage.
Thank you for learning with us.
Discover how LifeinCloud can power your projects with our flexible solutions for compute, storage, and networking.