Integrating GitLab with Ubuntu: A Step-by-Step Guide
GitLab is a powerful web-based Git repository manager that integrates seamlessly with Ubuntu, enabling version control, CI/CD, issue tracking, and collaboration. Below is a structured guide to installing, configuring, and optimizing GitLab on Ubuntu.
Before installing GitLab, ensure your Ubuntu system meets the following requirements:
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl openssh-server ca-certificates tzdata postfix
During postfix installation, select Internet Site and enter your domain/server name (e.g., gitlab.example.com
).GitLab CE is the free, open-source version suitable for most teams. Follow these steps:
GitLab provides a Debian repository to simplify installation and updates. Run the following commands to add the repository:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
This script adds GitLab’s GPG key and configures the repository.
Specify your server’s external URL (domain or IP address) during installation. Replace <YOUR_DOMAIN_OR_IP>
with your actual value (e.g., http://192.168.1.100
):
sudo EXTERNAL_URL="<YOUR_DOMAIN_OR_IP>" apt install gitlab-ce -y
The -y
flag auto-confirms installation prompts.
After installation, configure GitLab to suit your environment:
If you need to change the external URL later, edit the GitLab configuration file:
sudo nano /etc/gitlab/gitlab.rb
Find the line external_url '<YOUR_DOMAIN_OR_IP>'
and update it. Save the file and exit (Ctrl+O
, Enter
, Ctrl+X
).
Apply configuration changes by running:
sudo gitlab-ctl reconfigure
This command updates GitLab’s settings, restarts services, and ensures consistency.
To apply changes immediately, restart the GitLab service:
sudo gitlab-ctl restart
This ensures all components (web server, database, etc.) are running with the new configuration.
Once configured, access GitLab via your browser:
http://<YOUR_DOMAIN_OR_IP>
.root
/etc/gitlab/initial_root_password
(run cat /etc/gitlab/initial_root_password
to view; delete this file after use for security).Optimize GitLab for security and usability:
Log in as root
and immediately change the password. GitLab will prompt you to set a new one during the first login.
Use Let’s Encrypt to enable free SSL/TLS encryption. Install Certbot and run:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d <YOUR_DOMAIN>
Follow the prompts to complete the setup. This automatically configures HTTPS for your GitLab instance.
Allow HTTP (80), HTTPS (443), and SSH (22) ports to enable external access:
sudo ufw allow http
sudo ufw allow https
sudo ufw allow ssh
sudo ufw enable
Verify the rules with sudo ufw status
.
To push/pull code securely, add your SSH key to GitLab:
ssh-keygen -t rsa -C "your_email@example.com"
~/.ssh/id_rsa.pub
) to your clipboard.ssh -T git@<YOUR_DOMAIN>
You should see a welcome message (e.g., “Welcome to GitLab, @your_username!”).For production environments, consider these enhancements:
Schedule regular backups to protect your data. Run the following command to create a manual backup:
sudo gitlab-rake gitlab:backup:create
Backups are stored in /var/opt/gitlab/backups
by default. To automate backups, add a cron job (e.g., daily at 2 AM):
sudo crontab -e
Add this line:
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
Enable automated build/test/deploy pipelines by creating a .gitlab-ci.yml
file in your project root. Example:
stages:
- test
- deploy
test_job:
stage: test
script:
- echo "Running tests..."
- npm test # Replace with your test command
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- scp -r ./ user@server:/var/www/html # Replace with your deploy command
only:
- main # Run only on the main branch
Commit and push the file to trigger the pipeline.
By following these steps, you can successfully integrate GitLab with Ubuntu, creating a robust platform for source code management and team collaboration. For further customization, refer to GitLab’s official documentation.